﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using ZX.Web.UI.Controls;
using ZX.SYS.Library;
using ZX.BLL.Extend;
using ZX.DBModule;
using ZX.EHR.Library;
namespace ZX.Web.UI.Controls
{
	public partial class ChartReportQueryParams : BasePage
	{
        public string QueryBody
        {
            get;
            set;
        }
        protected void Page_Load(object sender, EventArgs e)
		{
            StringBuilder sb = new StringBuilder();
            //获取该页面上所有报表的查询参数
            string funId = GetStringFromQueryString("functionID", string.Empty);
            if (funId == string.Empty) return;
            SysChartReport sysChartReport = new SysChartReport();
            BaseBussnies bll = new BaseBussnies(sysChartReport);
            IList<BaseEntity> lists = bll.GetEntitysByCondition("MenuId='" + funId + "'");
            SysReportQueryParameter reportQueryParam = new SysReportQueryParameter();
            BaseBussnies queryParamBll = new BaseBussnies(reportQueryParam);
            foreach (BaseEntity entity in lists)
            {
                SysChartReport _report = (SysChartReport)entity;
                //获取当前报表的所有查询参数
                IList<BaseEntity> paramLists = queryParamBll.GetEntitysByCondition("ChartReportId='" + _report.ID + "' order by RecID");
                if (paramLists == null || paramLists.Count == 0) continue;
                sb.Append("<div class=\"SubTitle\" title='" + _report.ChartTitle + "报表查询参数'>" + _report.ChartTitle + "报表查询参数</div>");
                sb.Append("<div id='" + _report.ID.ToString() + "' class=\"QueryBody\"><table>");
                sb.Append("<tr>");
                if (paramLists.Count <= 3) //分成四列
                {
                    for (int i = 0; i < paramLists.Count; i++)
                    {
                        SysReportQueryParameter tempQueryParam = paramLists[i] as SysReportQueryParameter;
                        sb.Append("<td>" + tempQueryParam.DisplayName + "</td>");
                        if (tempQueryParam.ControlType == 1)
                        {
    
                            sb.Append("<td><input id='" + tempQueryParam.ParameterName + "' type='text' class='Wdate' onclick='WdatePicker()' style='width:160' value='" + tempQueryParam.DefaultValue + "' /></td>");
                        }
                        else if (tempQueryParam.ControlType == 2) //下拉框
                        { 
                        }
                        else if (tempQueryParam.ControlType == 3) //分公司下拉框
                        {
                            System.Text.StringBuilder sbFenGongSi = new StringBuilder();
                            
                            DatDepartment ddt= new DatDepartment();
                            bll.entity=ddt;
                            IList<BaseEntity> iDDT= bll.GetEntitysByCondition("(DeptName like '%分公司%' or DeptName like '%大区%') and LDeptName like '%营销中心%'order by DeptName");
                            if (iDDT!=null&&iDDT.Count>0)
                            {
                                foreach (BaseEntity dentity in iDDT)
                                {
                                    DatDepartment datDt = dentity as DatDepartment;
                                    string selected = string.Empty;
                                    if (datDt.DeptName == tempQueryParam.DefaultValue)
                                    {
                                        selected = " selected";
                                    }
                                    sbFenGongSi.Append("<option value='" + datDt.DeptName + "'" + selected + ">" + datDt.DeptName + "</option>");
                                }
                            }
                            sb.Append("<td><select id='" + tempQueryParam.ParameterName + "' style='width:160' value='" + tempQueryParam.DefaultValue + "'>"+sbFenGongSi.ToString()+"</select></td>");
                        }
                        else
                        {
                            switch (tempQueryParam.ParameterName)
                            {
                                case "@CurrentUserID"://当前用户ID
                                    tempQueryParam.DefaultValue = this.User.UserID.ToString();
                                    break;
                                case "@CureentUserName"://当前用户名
                                    tempQueryParam.DefaultValue = this.User.LoginName.ToString();
                                    break;
                                case "@CurrentUserDeptID"://当前用户ID
                                    tempQueryParam.DefaultValue = this.EmpPropertyValues.DeptID.ToLower();//this.GetDefCondition();
                                    break;
                                case "@CurrentUserDeptName"://当前用户部门长名称
                                    tempQueryParam.DefaultValue = this.EmpPropertyValues.LDeptName;
                                    break;
                                case "@CureetnTime"://当前时间
                                    tempQueryParam.DefaultValue = "getdate()";
                                    break;
                            }
                            sb.Append("<td><input id='" + tempQueryParam.ParameterName + "' type='text' style='width:160' value='" + tempQueryParam.DefaultValue + "'/></td>");
                        }
                    }
                    for (int i = 0; i < 3 - paramLists.Count; i++)
                    {
                        sb.Append("<td></td><td></td>");
                    }
                    sb.Append("<td><button id='btn" + _report.ID.ToString() + "' onclick='fnQuery(\"" + _report.ID.ToString() + "\");'>查询</button></td>");
                }
                else //每行三列
                {
                    for (int i = 0; i < paramLists.Count; i++)
                    {
                        SysReportQueryParameter tempQueryParam = paramLists[i] as SysReportQueryParameter;
                        if (i % 3 == 0)//需要换行
                        {
                            sb.Append("</tr><tr><td>" + tempQueryParam.DisplayName + "</td>");
                        }
                        else
                        {
                            sb.Append("<td>" + tempQueryParam.DisplayName + "</td>");
                        }
                        if (tempQueryParam.ControlType == 1)
                        {
                            sb.Append("<td><input id='" + tempQueryParam.ParameterName + "' type='text' class='Wdate' onclick='WdatePicker()' style='width:160' value='" + tempQueryParam.DefaultValue + "'/></td>");
                        }
                        else
                        {
                            sb.Append("<td><input id='" + tempQueryParam.ParameterName + "' type='text' style='width:160' value='" + tempQueryParam.DefaultValue + "'/></td>");
                        }
                    }
                    if (paramLists.Count % 3 != 0)
                    {
                        for (int i = 0; i < 3 - paramLists.Count % 3; i++)
                        {
                            sb.Append("<td></td><td></td>");
                        }
                    }
                    sb.Append("<td><button id='btn" + _report.ID.ToString() + "' onclick='fnQuery(\""+_report.ID.ToString()+"\");'>查询</button></td>");
                    sb.Append("</tr>");
                }
                sb.Append("</table></div>");
            }
            QueryBody = sb.ToString();
		}
	}
}